package edu.cornell.cubooks.dao.impl;

import java.util.List;
import java.util.Set;

import org.hibernate.criterion.Restrictions;
import org.springframework.transaction.annotation.Transactional;

import edu.cornell.cubooks.dao.FeedbackDao;
import edu.cornell.cubooks.domain.Feedback;
import edu.cornell.cubooks.domain.Posting;
import edu.cornell.cubooks.domain.User;

@Transactional
public class FeedbackDaoImpl extends GenericDaoImpl<Feedback, Integer> implements FeedbackDao {

	/*
	 * (non-Javadoc)
	 * @see edu.cornell.cubooks.dao.FeedbackDao#getFeedbacksForPosting(edu.cornell.cubooks.domain.Posting)
	 */
	@SuppressWarnings("unchecked")
	@Transactional(readOnly = false)
	public Set<Feedback> getFeedbacksForPosting(Posting posting) {
		List<Feedback> feedbacks = (List<Feedback>) sessionFactory.getCurrentSession()
			.createCriteria(domainClass)
			.add(Restrictions.eq("posting", posting))
			.list();
		return toSet(feedbacks);
	}
	
	/*
	 * (non-Javadoc)
	 * @see edu.cornell.cubooks.dao.FeedbackDao#getFeedbacksForSeller(edu.cornell.cubooks.domain.User)
	 */
	@SuppressWarnings("unchecked")
	@Transactional(readOnly = false)
	public Set<Feedback> getFeedbacksForSeller(User seller) {
		List<Feedback> feedbacks = (List<Feedback>) sessionFactory.getCurrentSession()
			.createCriteria(domainClass)
			.add(Restrictions.eq("user", seller))
			.list();
		return toSet(feedbacks);
	}
}